package com.design.fitment.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.design.fitment.R;
import com.design.fitment.adapter.GridViewAdapter;
import com.design.fitment.entity.HomeGallery;
import com.design.fitment.util.DesignNetworkUtil;
import com.design.fitment.util.RequestDataCallback;
import com.google.gson.Gson;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.view.annotation.ViewInject;

;import java.util.ArrayList;
import java.util.List;

public class FragmentGalleryPublic extends Fragment {
    private List<HomeGallery.DataEntity> entities;
    GridViewAdapter gridViewAdapter;

    @ViewInject(R.id.public_fragment_gridview)
    GridView gridView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View view  = inflater.inflate(R.layout.fragment__gallery_public,container,false);
        ViewUtils.inject(this, view);
        return view;
    }


    @Override
    public void onResume() {
        super.onResume();

        initView();
        load();
    }

    private void initView() {
        entities = new ArrayList<>();
        gridViewAdapter = new GridViewAdapter(getActivity(), entities);
        gridView.setAdapter(gridViewAdapter);


    }
    private void load() {

        //下载家装
        DesignNetworkUtil.getInstance().getGalleryPublic(new RequestDataCallback() {
            @Override
            public void onSuccess(String response) {
                Gson gson = new Gson();
                HomeGallery homeGallery = gson.fromJson(response, HomeGallery.class);
                if (homeGallery != null && homeGallery.getData() != null) {
                    entities.addAll(homeGallery.getData());
                    gridViewAdapter.notifyDataSetChanged();
                }


            }

            @Override
            public void onFailure(HttpException error) {

            }
        });
    }
}
